﻿Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Drawing.Imaging

Namespace helper
    Public Class ThumbnailImageHelper
        ' Methods
        Public Shared Function GetCodecInfo(ByVal mimeType As String) As ImageCodecInfo
            Dim info As ImageCodecInfo
            For Each info In ImageCodecInfo.GetImageEncoders
                If (info.MimeType = mimeType) Then
                    Return info
                End If
            Next
            Return Nothing
        End Function

        Public Shared Function GetImageCodecInfo(ByVal format As ImageFormat) As ImageCodecInfo
            Dim info As ImageCodecInfo
            For Each info In ImageCodecInfo.GetImageEncoders
                If (info.FormatID = format.Guid) Then
                    Return info
                End If
            Next
            Return Nothing
        End Function

        Public Shared Function GetThumbnailImage(ByVal image As Image, ByVal width As Integer, ByVal height As Integer) As Bitmap
            If (((image Is Nothing) OrElse (width < 1)) OrElse (height < 1)) Then
                Return Nothing
            End If
            Dim bitmap As New Bitmap(width, height)
            Using graphics As Graphics = graphics.FromImage(bitmap)
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
                graphics.SmoothingMode = SmoothingMode.HighQuality
                graphics.CompositingQuality = CompositingQuality.HighQuality
                graphics.Clear(Color.Transparent)
                graphics.DrawImage(image, New Rectangle(0, 0, width, height), New Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel)
                Return bitmap
            End Using
        End Function

        Public Shared Function GetThumbnailImageSize(ByVal image As Image, ByVal width As Integer, ByVal height As Integer) As Size
            Dim size As New Size(width, height)
            Dim num As Double = (CDbl(image.Height) / CDbl(image.Width))
            Dim num2 As Double = (CDbl(image.Width) / CDbl(image.Height))
            Dim num1 As Integer = size.Height
            Dim num3 As Integer = size.Width
            If (num2 > 0) Then
                size.Width = Convert.ToInt32(CDbl((size.Height * num2)))
            End If
            If (size.Width > num3) Then
                size.Width = num3
                size.Height = Convert.ToInt32(CDbl((size.Width * num)))
            End If
            Return size
        End Function

        Public Shared Sub SaveImage(ByVal image As Image, ByVal savePath As String, ByVal ici As ImageCodecInfo)
            Dim encoderParams As New EncoderParameters(1)
            Dim parameter As New EncoderParameter(Encoder.Quality, &H5F)
            encoderParams.Param(0) = parameter
            image.Save(savePath, ici, encoderParams)
            encoderParams.Dispose()
        End Sub

        Public Shared Sub SaveToThumbnailImage(ByVal image As Image, ByVal width As Integer, ByVal height As Integer, ByVal targetfile As String)
            Dim imageCodecInfo As ImageCodecInfo = ThumbnailImageHelper.GetImageCodecInfo(ImageFormat.Jpeg)
            Dim size As Size = ThumbnailImageHelper.GetThumbnailImageSize(image, width, height)
            Using bitmap As Bitmap = ThumbnailImageHelper.GetThumbnailImage(image, size.Width, size.Height)
                ThumbnailImageHelper.SaveImage(bitmap, targetfile, imageCodecInfo)
            End Using
        End Sub
    End Class
End Namespace


